package com.befirefly.studentmanager.view;

import com.befirefly.studentmanager.entity.StudentDO;
import com.befirefly.studentmanager.handler.UpdateStudentViewHandler;
import com.befirefly.studentmanager.service.StudentService;
import com.befirefly.studentmanager.service.impl.StudentServiceImpl;

import javax.swing.*;
import java.awt.*;

/**
 * @Author: 八千里路云和月
 * @Date: 2021/2/9 13:48
 * @Description:
 */
public class UpdateStudentView extends JDialog {

    JPanel jPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 10, 20));
    JLabel idLabel = new JLabel("学生编号：");
    JTextField idTxt = new JTextField();
    JLabel nameLabel = new JLabel("姓名：");
    JTextField nameTxt = new JTextField();
    JLabel noLabel = new JLabel("学号：");
    JTextField noTxt = new JTextField("");
    JLabel homeLabel = new JLabel("家乡：");
    JTextField homeTxt = new JTextField();
    JLabel cnLabel = new JLabel("语文成绩：");
    JTextField cnTxt = new JTextField();
    JLabel enLabel = new JLabel("英语成绩：");
    JTextField enTxt = new JTextField();
    JLabel mathLabel = new JLabel("数学成绩：");
    JTextField mathTxt = new JTextField();
    JButton updateBtn = new JButton("修改");

    UpdateStudentViewHandler updateStudentViewHandler;

    public UpdateStudentView(MainView mainView, long selectedStudentId) {
        super(mainView, "修改学生", true);

        // 数据查询对应的记录并回显
        updateStudentViewHandler = new UpdateStudentViewHandler(this, mainView);
        StudentService studentService = new StudentServiceImpl();
        StudentDO selectedStu = studentService.selectById(selectedStudentId);
        idLabel.setPreferredSize(new Dimension(80, 30));
        jPanel.add(idLabel);
        idTxt.setPreferredSize(new Dimension(200, 30));
        idTxt.setText(String.valueOf(selectedStu.getId()));
        // 设置id不可编辑
        idTxt.setEditable(false);
        jPanel.add(idTxt);

        nameLabel.setPreferredSize(new Dimension(80, 30));
        jPanel.add(nameLabel);
        nameTxt.setPreferredSize(new Dimension(200, 30));
        nameTxt.setText(selectedStu.getName());
        jPanel.add(nameTxt);

        noLabel.setPreferredSize(new Dimension(80, 30));
        jPanel.add(noLabel);
        noTxt.setPreferredSize(new Dimension(200, 30));
        noTxt.setText(selectedStu.getNo());
        jPanel.add(noTxt);

        homeLabel.setPreferredSize(new Dimension(80, 30));
        jPanel.add(homeLabel);
        homeTxt.setPreferredSize(new Dimension(200, 30));
        homeTxt.setText(selectedStu.getHome());
        jPanel.add(homeTxt);

        cnLabel.setPreferredSize(new Dimension(80, 30));
        jPanel.add(cnLabel);
        cnTxt.setPreferredSize(new Dimension(200, 30));
        cnTxt.setText(String.valueOf(selectedStu.getCnScore()));
        jPanel.add(cnTxt);

        enLabel.setPreferredSize(new Dimension(80, 30));
        jPanel.add(enLabel);
        enTxt.setPreferredSize(new Dimension(200, 30));
        enTxt.setText(String.valueOf(selectedStu.getEnScore()));
        jPanel.add(enTxt);

        mathLabel.setPreferredSize(new Dimension(80, 30));
        jPanel.add(mathLabel);
        mathTxt.setPreferredSize(new Dimension(200, 30));
        mathTxt.setText(String.valueOf(selectedStu.getMathScore()));
        jPanel.add(mathTxt);

        updateBtn.addActionListener(updateStudentViewHandler);
        jPanel.add(updateBtn);

        Container contentPane = getContentPane();
        contentPane.add(jPanel);


        setSize(350, 450);
        setLocationRelativeTo(null);
        // DISPOSE_ON_CLOSE:只销毁当前的窗体
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setResizable(false);
        setVisible(true);
    }

    /*
    获取修改后的学生对象
     */
    public StudentDO buildUpdatedStudentDO() {
        StudentDO studentDO = new StudentDO();
        studentDO.setId(Long.valueOf(idTxt.getText()));
        studentDO.setName(nameTxt.getText());
        studentDO.setNo(noTxt.getText());
        studentDO.setHome(homeTxt.getText());
        studentDO.setCnScore(Integer.valueOf(cnTxt.getText()));
        studentDO.setEnScore(Integer.valueOf(enTxt.getText()));
        studentDO.setMathScore(Integer.valueOf(mathTxt.getText()));
        return studentDO;

    }
}
